<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/23
 * Time: 14:26
 */

namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use app\portal\model\ActivityModel;

class AdminMapController extends AdminBaseController
{
    public function index(){
        $model=new ActivityModel();
        $act=$model->with('user')->with('log')->order('id desc')->select();

        if($this->request->isGet()){
            $this->assign('act',$act);
            return $this->fetch('map');
        }else{
            $index=$this->request->param('index');
           if($index==0){
               $act=$model->with('user')->with('log')->order('id desc')->find();
           }else{
               $act=$model->where('id',$index)->with('user')->with('log')->find();
           }
            $time_interval =  $act['time'];
            $now=time();
            $time_interval=json_decode($time_interval);
            $now_interval=[0,0];
            foreach ($time_interval as $k=>$v){
                if(time()>$act['start_date']&&time()<$act['end_date']+60*60*24){
                    $start_time=strtotime($v[0]);
                    $end_time=strtotime($v[1]);
                    if($start_time-15*60<=$now&&$end_time+30*60>$now){
                        $now_interval=[$start_time,$end_time];
                    }
                }
            }
            $point=json_decode($act['point']);
            $a=[];$b=[];
            foreach ($point as $k=>$v){
               $a[]=explode(',',$v)[0];
                $b[]=explode(',',$v)[1];
            }
            $c=(max($a)+min($a))/2;
            $d=(max($b)+min($b))/2;
            $point=$d.','.$c;
            $array=$act['log'];
            $info=[];
            foreach ($array as $ke=>$va){
                if($va['start_time']>$now_interval[0]-15*60&&$va['start_time']<$now_interval[1]+30*60){
                    $info[$ke]=explode(',',$va['location']);
                    $info[$ke][]=$va['user']['user_nickname'];
                }
            }
            $info=array_values($info);
            $data=['info'=>$info,'title'=>$act['title'],'point'=>$point];
            return $data;
        }
    }

}